package com.tchhy.mvplibrary.rx

import com.tchhy.mvplibrary.presenter.view.BaseView
import com.tchhy.provider.rx.RxErrorHandle
import com.trello.rxlifecycle2.LifecycleProvider
import io.reactivex.SingleObserver
import io.reactivex.disposables.Disposable

open class DefaultSingleObserver<T>(val lifecycleProvider: LifecycleProvider<*>,
                                    val view: BaseView?,
                                    private val onSuccess: (T) -> Unit) : SingleObserver<T> {
    override fun onSuccess(t: T) {
        onSuccess.invoke(t)
        view?.dismissLoading()
    }

    override fun onSubscribe(d: Disposable) {
        view?.showLoading()
    }

    override fun onError(e: Throwable) {
        view?.dismissLoading()
        view?.onError(RxErrorHandle(e).error())
    }
}